#!/bin/bash
{
	#////////////////////////////////////
	# DietPi JustBoom
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Tweak your high-end JustBoom soundcard
	# Usage:
	# - /boot/dietpi/misc/dietpi-justboom
	#////////////////////////////////////

	# Import DietPi-Globals --------------------------------------------------------------
	. /boot/dietpi/func/dietpi-globals
	readonly G_PROGRAM_NAME='DietPi-JustBoom'
	G_CHECK_ROOT_USER
	G_CHECK_ROOTFS_RW
	G_INIT
	# Import DietPi-Globals --------------------------------------------------------------

	# Prereq ALSA installed from dietpi-software
	if [[ ! -f '/etc/asound.conf' ]] || ! dpkg-query -s alsa-utils &> /dev/null; then

		G_WHIP_MSG "$G_PROGRAM_NAME requires ALSA to be installed and a soundcard to be configured.\n
Please select a soundcard via 'dietpi-config' or install ALSA, or ideally an audio application, through 'dietpi-software' before use."
		exit 1

	fi

	#/////////////////////////////////////////////////////////////////////////////////////
	# Script Globals
	#/////////////////////////////////////////////////////////////////////////////////////
	SOUNDCARD_CURRENT=
	SOUNDCARD_CARD_INDEX=0
	EQ_ENABLED=0

	# MPD
	OUTPUT_FREQ_CURRENT=0
	OUTPUT_FREQ_TARGET=0
	OUTPUT_BITRATE_CURRENT=0
	OUTPUT_BITRATE_TARGET=0
	SOXR_ENABLED=0
	SOXR_QUALITY=0
	MPD_AUDIO_BUFFER_SIZE=0
	readonly FP_MPD_CONF='/etc/mpd.conf'
	MPD_INSTALLED=0
	[[ -f $FP_MPD_CONF ]] && MPD_INSTALLED=1

	# CAVA
	CAVA_ENABLED=1
	CAVA_FRAMERATE=0
	CAVA_COLOUR_FG=0
	CAVA_COLOUR_BG=0
	CAVA_BAR_GRAVITY=0
	CAVA_BAR_WIDTH=0
	CAVA_MAINSCREENINUSE_DISPLAYED=0
	readonly FP_CAVA_CONF='/root/.config/cava/config'
	CAVA_INSTALLED=0
	[[ -f $FP_CAVA_CONF ]] && CAVA_INSTALLED=1

	Reset_Globals(){

		# ALSA EQ levels
		[[ -f '/var/lib/dietpi/dietpi-config/.alsaequal.bin' ]] && rm /var/lib/dietpi/dietpi-config/.alsaequal.bin

		# MPD
		OUTPUT_FREQ_TARGET=44100
		OUTPUT_BITRATE_TARGET=16
		SOXR_ENABLED=0
		SOXR_QUALITY='very high'
		EQ_ENABLED=0
		MPD_AUDIO_BUFFER_SIZE=2048

		# CAVA
		CAVA_ENABLED=1
		CAVA_FRAMERATE=30
		CAVA_COLOUR_FG='green'
		CAVA_COLOUR_BG='black'
		CAVA_BAR_GRAVITY='1.75'
		CAVA_BAR_WIDTH=2

		# Save changes
		Write_Apply_Globals

	}

	Read_Globals(){

		SOUNDCARD_CURRENT=$(sed -n '/^[[:blank:]]*CONFIG_SOUNDCARD=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)
		# Remove equalizer and conversion plugin siffix
		SOUNDCARD_CURRENT=${SOUNDCARD_CURRENT%-eq} SOUNDCARD_CURRENT=${SOUNDCARD_CURRENT%-plug}
		SOUNDCARD_CARD_INDEX=$(mawk '/card /{print $2;exit}' /etc/asound.conf)
		EQ_ENABLED=$(grep -cm1 '^pcm.plugequal' /etc/asound.conf)

		# MPD specific
		if (( $MPD_INSTALLED )); then

			OUTPUT_FREQ_CURRENT=$(grep -m1 '^format' $FP_MPD_CONF | sed 's/\"//g' | sed 's/:/ /g' | mawk '{print $2}')
			OUTPUT_FREQ_TARGET=$OUTPUT_FREQ_CURRENT
			OUTPUT_BITRATE_CURRENT=$(grep -m1 '^format' $FP_MPD_CONF | sed 's/\"//g' | sed 's/:/ /g' | mawk '{print $3}')
			OUTPUT_BITRATE_TARGET=$OUTPUT_BITRATE_CURRENT
			SOXR_ENABLED=$(grep -cm1 '^samplerate_converter \"soxr' $FP_MPD_CONF)
			SOXR_QUALITY=$(grep -m1 'samplerate_converter \"soxr' $FP_MPD_CONF | sed 's/\"//g' | mawk '{for(i=3;i<=NF;i++){printf "%s ", $i}; printf "\n"}' | sed 's/ *$//')
			MPD_AUDIO_BUFFER_SIZE=$(grep -m1 '^audio_buffer_size' $FP_MPD_CONF | mawk '{print $2}' | sed 's/\"//g')

		fi

		# CAVA specific
		if (( $CAVA_INSTALLED )); then

			CAVA_FRAMERATE=$(mawk '/framerate =/{print $NF;exit}' $FP_CAVA_CONF)
			CAVA_COLOUR_BG=$(mawk '/background =/{print $NF;exit}' $FP_CAVA_CONF)
			CAVA_COLOUR_FG=$(mawk '/foreground =/{print $NF;exit}' $FP_CAVA_CONF)
			CAVA_BAR_GRAVITY=$(mawk '/gravity =/{print $NF;exit}' $FP_CAVA_CONF)
			CAVA_BAR_WIDTH=$(mawk '/bar_width =/{print $NF;exit}' $FP_CAVA_CONF)

		fi

	}

	Write_Apply_Globals(){

		if (( $EQ_ENABLED )); then

			/boot/dietpi/func/dietpi-set_hardware soundcard "$SOUNDCARD_CURRENT-eq"

		else

			/boot/dietpi/func/dietpi-set_hardware soundcard "$SOUNDCARD_CURRENT"

		fi

		# MPD specific
		if (( $MPD_INSTALLED )); then

			# Output freq/bit
			sed -i "/^format \"/c\format \"$OUTPUT_FREQ_TARGET:$OUTPUT_BITRATE_TARGET:2\"" $FP_MPD_CONF

			if (( $SOXR_ENABLED )); then

				sed -i "/samplerate_converter \"/c\samplerate_converter \"soxr $SOXR_QUALITY\"" $FP_MPD_CONF
				sed -i "/audio_output_format \"/c\audio_output_format \"$OUTPUT_FREQ_TARGET:$OUTPUT_BITRATE_TARGET:2\"" $FP_MPD_CONF

			else

				sed -i "/samplerate_converter \"/c\#samplerate_converter \"soxr $SOXR_QUALITY\"" $FP_MPD_CONF
				sed -i "/audio_output_format \"/c\#audio_output_format \"$OUTPUT_FREQ_TARGET:$OUTPUT_BITRATE_TARGET:2\"" $FP_MPD_CONF

			fi

			# Buffer
			sed -i "/^audio_buffer_size \"/c\audio_buffer_size \"$MPD_AUDIO_BUFFER_SIZE\"" $FP_MPD_CONF

			systemctl restart mpd

		fi

		# CAVA specific
		if (( $CAVA_INSTALLED )); then

			G_CONFIG_INJECT 'framerate =' "framerate = $CAVA_FRAMERATE" $FP_CAVA_CONF
			G_CONFIG_INJECT 'background =' "background = $CAVA_COLOUR_BG" $FP_CAVA_CONF
			G_CONFIG_INJECT 'foreground =' "foreground = $CAVA_COLOUR_FG" $FP_CAVA_CONF
			G_CONFIG_INJECT 'gravity =' "gravity = $CAVA_BAR_GRAVITY" $FP_CAVA_CONF
			G_CONFIG_INJECT 'bar_width =' "bar_width = $CAVA_BAR_WIDTH" $FP_CAVA_CONF

			Cava_Start

		fi

		Read_Globals

	}

	Cava_Start(){

		killall -qw cava

		(( $CAVA_ENABLED )) || return

		# Running on current screen
		if [[ $(tty) == '/dev/tty1' ]]; then

			# Display this warning once only
			(( $CAVA_MAINSCREENINUSE_DISPLAYED )) && return

			G_WHIP_MSG "Error:\n\nMain screen in use. To launch CAVA on the main screen, please exit $G_PROGRAM_NAME and run the following command:\n - cava\n\nNB: Press CTRL+C to exit cava."
			CAVA_MAINSCREENINUSE_DISPLAYED=1

		else

			setterm -blank 0 -powersave off <> /dev/fb2 >&0 2>&1 # Odroid 3.5 inch
			cava <> /dev/console >&0 2>&1 &

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Whip
	#/////////////////////////////////////////////////////////////////////////////////////
	TARGETMENUID=0
	LAST_SELECTED_NAME=0

	Menu_Exit(){

		G_WHIP_SIZE_X_MAX=50
		G_WHIP_YESNO "Exit $G_PROGRAM_NAME?" && TARGETMENUID=-1 # Exit

	}

	#TARGETMENUID=0
	Menu_Main(){

		Read_Globals

		local eq_enabled_text='Off'
		(( $EQ_ENABLED )) && eq_enabled_text='On'

		G_WHIP_MENU_ARRAY=('' '●─ Global Options ')
		G_WHIP_MENU_ARRAY+=('ALSA Mixer' ': Launches ALSA Mixer, tweak sound card settings')
		G_WHIP_MENU_ARRAY+=('ALSA Output Info' ': View info on current ALSA playback stream')
		G_WHIP_MENU_ARRAY+=("EQ Enable" ": [$eq_enabled_text] | Select to toggle EQ")
		(( $EQ_ENABLED )) && G_WHIP_MENU_ARRAY+=('EQ Configure' ': Launches AlsaMixer to configure EQ settings')
		G_WHIP_MENU_ARRAY+=('Reset' ': Reset all options to defaults')

		# Allo Piano 2.1 specials
		if grep -qi 'piano-dac-plus' <<< "$SOUNDCARD_CURRENT"; then

			G_WHIP_MENU_ARRAY+=('' '●─ Allo Piano 2.1 ')

			local dsp_filter=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'DSP Program'" | grep 'Item0:' | sed 's/.*Item0: //')
			G_WHIP_MENU_ARRAY+=("DSP Filter" ": $dsp_filter")

			local digital_volume_int=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Digital'" | grep 'Front Left:' | mawk '{print $4}')
			local digital_volume_db=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Digital'" | grep 'Front Left:' | mawk '{print $6}' | tr -d '\[\]')
			local subwoofer_mode=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Subwoofer mode'" | grep 'Item0:' | sed 's/.*Item0: //')
			local subwoofer_volume_int=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Subwoofer'" | grep 'Front Left:' | mawk '{print $4}')
			local subwoofer_volume_db=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Subwoofer'" | grep 'Front Left:' | mawk '{print $6}' | tr -d '\[\]')
			local subwoofer_crossover_frequency=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Lowpass'" | grep 'Item0:' | sed 's/.*Item0: //')
			local dualoutput_mode=$(amixer -c "$SOUNDCARD_CARD_INDEX" sget "'Dual Mode'" | grep 'Item0:' | sed 's/.*Item0: //')

			G_WHIP_MENU_ARRAY+=('Master Volume' ": $digital_volume_int ($digital_volume_db)")

			G_WHIP_MENU_ARRAY+=('Dual Output Mode' ": $dualoutput_mode")
			G_WHIP_MENU_ARRAY+=('Subwoofer Mode' ": $subwoofer_mode")
			G_WHIP_MENU_ARRAY+=('Subwoofer Volume' ": $subwoofer_volume_int ($subwoofer_volume_db)")

			# - Only active with subwoofer 2.1+ modes
			[[ $subwoofer_mode != '2.0' ]] && G_WHIP_MENU_ARRAY+=('Subwoofer Crossover' ": $subwoofer_crossover_frequency Hz")

		fi

		# MPD specific
		if (( $MPD_INSTALLED )); then

			G_WHIP_MENU_ARRAY+=('' '●─ MPD Specific ')
			G_WHIP_MENU_ARRAY+=('Output Frequency' ": $OUTPUT_FREQ_CURRENT Hz | Select to change")
			G_WHIP_MENU_ARRAY+=('Output BitRate' ": $OUTPUT_BITRATE_CURRENT Bit | Select to change")
			G_WHIP_MENU_ARRAY+=('Audio Buffer' ": $MPD_AUDIO_BUFFER_SIZE Bytes | Select to change")

			local soxr_enabled_text='Off'
			(( $SOXR_ENABLED )) && soxr_enabled_text='On'
			G_WHIP_MENU_ARRAY+=('SOXR Enable' ": [$soxr_enabled_text] | Select to toggle SOXR resampling")
			(( $SOXR_ENABLED )) && G_WHIP_MENU_ARRAY+=('SOXR Quality' ": $SOXR_QUALITY | Select SOXR quality level")

		fi

		# CAVA specific
		if (( $CAVA_INSTALLED )); then

			G_WHIP_MENU_ARRAY+=('' '●─ CAVA Specific ')
			G_WHIP_MENU_ARRAY+=('Cava Start/Restart' ': Launch Cava on main screen')
			pgrep 'cava' &> /dev/null && G_WHIP_MENU_ARRAY+=('Cava Stop' ': Stop Cava')
			G_WHIP_MENU_ARRAY+=('Cava Framerate' ": $CAVA_FRAMERATE fps | Select to change")
			G_WHIP_MENU_ARRAY+=('Cava FG Colour' ": $CAVA_COLOUR_FG | Select to change")
			G_WHIP_MENU_ARRAY+=('Cava BG Colour' ": $CAVA_COLOUR_BG | Select to change")
			G_WHIP_MENU_ARRAY+=('Cava Bar Width' ": $CAVA_BAR_WIDTH | Select to change")
			G_WHIP_MENU_ARRAY+=('Cava Bar Gravity' ": $CAVA_BAR_GRAVITY | Select to change")

		fi

		G_WHIP_DEFAULT_ITEM=$LAST_SELECTED_NAME
		G_WHIP_BUTTON_CANCEL_TEXT='Exit'
		if G_WHIP_MENU 'Tweak your High-End Audiophile JustBoom (and others) sound card:'; then

			LAST_SELECTED_NAME=$G_WHIP_RETURNED_VALUE

			if [[ $G_WHIP_RETURNED_VALUE == 'ALSA Output Info' ]]; then

				G_DIETPI-NOTIFY 3 DietPi-JustBoom "ALSA Output Info"
				local stream_result=$(cat /proc/asound/card*/pcm0p/sub0/hw_params)
				echo -e "$stream_result\n"

				[[ $stream_result == 'closed' ]] && G_DIETPI-NOTIFY 1 'Detailed info is only available when audio is being played, play some music first!'

				G_DIETPI-NOTIFY 0 'Completed, press any key to exit...\n'
				read -rp 'Press any key to exit...'

			elif [[ $G_WHIP_RETURNED_VALUE == 'Subwoofer Mode' ]]; then

				G_WHIP_MENU_ARRAY=(

					'2.0' ': Disables Subwoofer output'
					'2.1' ': Enables Subwoofer output (mono)'
					'2.2' ': Enables Subwoofer output (stereo)'

				)

				G_WHIP_DEFAULT_ITEM=$subwoofer_mode
				G_WHIP_MENU 'Please select a channel mode for Subwoofer output:' && amixer -c "$SOUNDCARD_CARD_INDEX" sset 'Subwoofer mode' "$G_WHIP_RETURNED_VALUE"

			elif [[ $G_WHIP_RETURNED_VALUE == 'Dual Output Mode' ]]; then

				G_WHIP_MENU_ARRAY=(

					'Dual-Mono' ': Enables Dual output (mono)'
					'Dual-Stereo' ': Enables Dual output (stereo)'

				)

				G_WHIP_DEFAULT_ITEM=$dualoutput_mode
				G_WHIP_MENU 'Please select a channel mode for Subwoofer output:' && amixer -c "$SOUNDCARD_CARD_INDEX" sset 'Dual Mode' "$G_WHIP_RETURNED_VALUE"

			elif [[ $G_WHIP_RETURNED_VALUE == 'Master Volume' ]]; then

				G_WHIP_MENU_ARRAY=()

				for ((i=206; i>=0; i--))
				do

					if (( $i == 0 )); then

						G_WHIP_MENU_ARRAY+=("$i" 'Mute')

					elif (( $i == 206 )); then

						G_WHIP_MENU_ARRAY+=("$i" ': Max (-0.50dB)')

					else

						G_WHIP_MENU_ARRAY+=("$i" '')

					fi

				done

				G_WHIP_DEFAULT_ITEM=$digital_volume_int
				G_WHIP_MENU 'Please select a volume level for Master output:' && amixer -c "$SOUNDCARD_CARD_INDEX" sset 'Digital' "$G_WHIP_RETURNED_VALUE"

			elif [[ $G_WHIP_RETURNED_VALUE == 'Subwoofer Volume' ]]; then

				G_WHIP_MENU_ARRAY=()

				for ((i=206; i>=0; i--))
				do

					if (( $i == 0 )); then

						G_WHIP_MENU_ARRAY+=("$i" 'Mute')

					elif (( $i == 206 )); then

						G_WHIP_MENU_ARRAY+=("$i" ': Max (-0.50dB)')

					else

						G_WHIP_MENU_ARRAY+=("$i" '')

					fi

				done

				G_WHIP_DEFAULT_ITEM=$subwoofer_volume_int
				G_WHIP_MENU 'Please select a volume level for Subwoofer output:' && amixer -c "$SOUNDCARD_CARD_INDEX" sset 'Subwoofer' "$G_WHIP_RETURNED_VALUE"

			elif [[ $G_WHIP_RETURNED_VALUE == 'Subwoofer Crossover' ]]; then

				G_WHIP_MENU_ARRAY=(

					'60' 'Hz'
					'70' 'Hz'
					'80' 'Hz'
					'90' 'Hz (Default)'
					'100' 'Hz'
					'110' 'Hz'
					'120' 'Hz'
					'130' 'Hz'
					'140' 'Hz'
					'150' 'Hz'
					'160' 'Hz'
					'170' 'Hz'
					'180' 'Hz'
					'190' 'Hz'
					'200' 'Hz'

				)

				G_WHIP_DEFAULT_ITEM=$subwoofer_crossover_frequency
				G_WHIP_MENU 'Please select the lowpass crossover frequency for Subwoofer:\n
Everything below the selected freq will be output through subwoofer out, when subwoofer mode 2.1/2.2 is selected.' && amixer -c "$SOUNDCARD_CARD_INDEX" sset 'Lowpass' "$G_WHIP_RETURNED_VALUE"

			elif [[ $G_WHIP_RETURNED_VALUE == 'DSP Filter' ]]; then

				G_WHIP_MENU_ARRAY=(

					'FIR interpolation with de-emphasis' '(Default)'
					'Low latency IIR with de-emphasis' ''
					'High attenuation with de-emphasis' ''
					'Fixed process flow' ''
					'Ringing-less low latency FIR' ''

				)

				G_WHIP_DEFAULT_ITEM=$dsp_filter
				G_WHIP_MENU 'Please select a DSP Filter:' && amixer -c "$SOUNDCARD_CARD_INDEX" sset 'DSP Program' "$G_WHIP_RETURNED_VALUE"

			elif [[ $G_WHIP_RETURNED_VALUE == 'Cava Start/Restart' ]]; then

				Cava_Start

			elif [[ $G_WHIP_RETURNED_VALUE == 'Cava Stop' ]]; then

				killall -w cava

			elif [[ $G_WHIP_RETURNED_VALUE == 'Cava Framerate' ]]; then

				G_WHIP_MENU_ARRAY=(

					'10' ': Lowest CPU usage'
					'20' ''
					'30' ': Default'
					'40' ''
					'50' ''
					'60' ''
					'80' ''
					'100' ''
					'120' ': Highest CPU usage'

				)

				G_WHIP_DEFAULT_ITEM=$CAVA_FRAMERATE
				if G_WHIP_MENU 'Please select a framerate:'; then

					CAVA_FRAMERATE=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Cava Bar Width' ]]; then

				G_WHIP_MENU_ARRAY=(

					'1' ': Thin (Increases total bar count)'
					'2' ': Default'
					'3' ''
					'4' ''
					'5' ''
					'6' ''
					'8' ': Thick (Lowers total bar count)'

				)

				G_WHIP_DEFAULT_ITEM=$CAVA_BAR_WIDTH
				if G_WHIP_MENU 'Please select a bar width:\n - The size of the bars affects how many are displayed'; then

					CAVA_BAR_WIDTH=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Cava FG Colour' || $G_WHIP_RETURNED_VALUE == 'Cava BG Colour' ]] ; then

				local apply_fg_colour=0
				local default_item=$CAVA_COLOUR_BG
				if [[ $G_WHIP_RETURNED_VALUE == 'Cava FG Colour' ]]; then

					apply_fg_colour=1
					default_item=$CAVA_COLOUR_FG

				fi

				G_WHIP_MENU_ARRAY=(

					'green' ''
					'red' ''
					'yellow' ''
					'magenta' ''
					'cyan' ''
					'white' ''
					'blue' ''
					'black' ''

				)

				G_WHIP_DEFAULT_ITEM=$default_item
				if G_WHIP_MENU 'Please select a colour:'; then

					(( $apply_fg_colour )) && CAVA_COLOUR_FG=$G_WHIP_RETURNED_VALUE || CAVA_COLOUR_BG=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Cava Bar Gravity' ]]; then

				G_WHIP_MENU_ARRAY=(

					'0.1' ': Slow drop off speed'
					'0.25' ''
					'0.5' ''
					'0.75' ''
					'1' ''
					'1.25' ''
					'1.5' ''
					'1.75' ': Default'
					'2' ': High drop off speed'

				)

				G_WHIP_DEFAULT_ITEM=$CAVA_BAR_GRAVITY
				if G_WHIP_MENU 'Please select a gravity level (drop off speed) for the bars:\n - Higher values means bars will drop faster.'; then

					CAVA_BAR_GRAVITY=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'EQ Configure' ]]; then

				if (( $EQ_ENABLED == 1 )); then

					G_WHIP_MSG 'The ALSA EQ mixer will now be launched, when finished, press F10 or ESC to exit.'
					alsamixer -D eq
					G_EXEC chown dietpi:dietpi /var/lib/dietpi/dietpi-config/.alsaequal.bin
					G_EXEC chmod 0664 /var/lib/dietpi/dietpi-config/.alsaequal.bin

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'ALSA Mixer' ]]; then

				G_WHIP_MSG 'ALSA Mixer will now be launched, when finished, press F10 or ESC to exit.'
				alsamixer

			elif [[ $G_WHIP_RETURNED_VALUE == 'EQ Enable' ]]; then

				(( $EQ_ENABLED )) && EQ_ENABLED=0 || EQ_ENABLED=1
				Write_Apply_Globals

			elif [[ $G_WHIP_RETURNED_VALUE == 'SOXR Enable' ]]; then

				(( $SOXR_ENABLED )) && SOXR_ENABLED=0 || SOXR_ENABLED=1
				Write_Apply_Globals

			elif [[ $G_WHIP_RETURNED_VALUE == 'SOXR Quality' ]]; then

				G_WHIP_MENU_ARRAY=(

					'very high' ': Highest quality, highest CPU usage'
					'high' ': Default quality, medium CPU usage'
					'low' ': Lowest quality, minimal CPU usage'

				)

				G_WHIP_DEFAULT_ITEM=$SOXR_QUALITY
				if G_WHIP_MENU 'Please select a SOXR quality level:'; then

					SOXR_QUALITY=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Audio Buffer' ]]; then

				G_WHIP_MENU_ARRAY=(

					'128' ': Lowest latency (may cause buffer under/overruns)'
					'192' ':'
					'256' ':'
					'384' ':'
					'512' ':'
					'1024' ':'
					'2048' ': Default'
					'4096' ': Highest latency'

				)

				G_WHIP_DEFAULT_ITEM=$MPD_AUDIO_BUFFER_SIZE
				if G_WHIP_MENU 'Please select a audio buffer size (bytes):'; then

					MPD_AUDIO_BUFFER_SIZE=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Output Frequency' ]]; then

				G_WHIP_MENU_ARRAY=(

					'384000' ': 384kHz (highest quality)'
					'352800' ': 352kHz'
					'192000' ': 192kHz'
					'96000' ': 96kHz'
					'88200' ': 88.2kHz'
					'64000' ': 64kHz'
					'48000' ': 48kHz (HDMI standard)'
					'44100' ': 44.1kHz (music standard)'

				)

				G_WHIP_DEFAULT_ITEM=$OUTPUT_FREQ_CURRENT
				if G_WHIP_MENU 'Please select a output frequency:'; then

					OUTPUT_FREQ_TARGET=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Output BitRate' ]]; then

				G_WHIP_MENU_ARRAY=(

					'32' ': (highest quality, recommended)'
					'24' ': '
					'16' ': (CD audio quality)'

				)

				G_WHIP_DEFAULT_ITEM=$OUTPUT_BITRATE_CURRENT
				if G_WHIP_MENU 'Please select a output bitrate:'; then

					OUTPUT_BITRATE_TARGET=$G_WHIP_RETURNED_VALUE
					Write_Apply_Globals

				fi

			elif [[ $G_WHIP_RETURNED_VALUE == 'Reset' ]]; then

				Reset_Globals

			fi

		else

			Menu_Exit

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	#-----------------------------------------------------------------------------------
	Read_Globals
	#-----------------------------------------------------------------------------------
	until (( $TARGETMENUID < 0 ))
	do

		Menu_Main

	done
	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}
